<?php
/**
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2017/1/3
 * Time: 9:35
 */

namespace Home\Controller;


use Common\Model\GoodsListsModel;
use Common\Model\GoodsModel;
use Common\Model\TypeAttrModel;
use Org\Util\Cart;
use Think\Controller;

class ContentController extends  Controller
{
    public function index(){
        //获得当前商品数据
        $gid = I('get.gid');
        //将商品表和商品详情表关联
        $data = (new GoodsModel())->alias('g')->join("shop_goodsdetail gd on g.gid=gd.gid ")->where("g.gid={$gid}")->find();
            $data['small']=explode(',',$data['small']);
            $data['medium']=explode(',',$data['medium']);
            $data['big']=explode(',',$data['big']);
            //分配数据
        $this->assign('data',$data);

        //处理规格属性 需要关联商品属性表和类型属性表
        $specData = (new TypeAttrModel())->alias('ta')->join('shop_goods_attr ga on ta.taid=ga.taid')->where("ga.gid={$gid} and ta.type=1")->select();
        //dd($specData);die;
        //重组数组
        $temp=[];
        foreach($specData as $k=>$v){
            $temp[$v['name']][] = $v;
        }
        //dd($temp);die;
        $this->assign('temp',$temp);

        //分配头部数据
        $conf = [
            'js'=>['list'],
            'css'=>['index']
        ];
        $this->assign('conf',$conf);
        //加载模板
        $this->display();
    }

    /**
     * 异步回去货品货存
     */
    public  function ajaxGetTotal(){
        if(IS_AJAX){
            //dd($_POST);
            $data['gid'] = I('post.gid');
            $data['grid']= rtrim(I('post.grid'),',');
            //根据两个参数在货品列表中查找货品库存
            $data = (new GoodsListsModel())->where("gid={$data['gid']} and grid='{$data['grid']}'")->find();
            //没有查到数据，库存视为0
            if(!$data){
                echo json_encode(['valid'=>0,'message'=>'']);die;

            }
            //有数据，但库存为0
            if($data['stocks']==0){
                echo json_encode(['valid'=>0,'message'=>'']);die;
            }
            //能走到这里就说明，有库存
            echo json_encode(['valid'=>1,'message'=>'','stocks'=>$data['stocks']]);
            //dd($data);die;
        }
    }

    /**
     * 异步添加购物车
     */

    public function ajaxAddCart(){
        if(IS_AJAX){
            $gid = $_POST['gid'];
            $data['num'] = $_POST['num'];
            $data['options'] = $_POST['options'];
            //根据商品id在商品表中获得商品名称以及价格
            $oneGoodsData = (new GoodsModel())->where("gid={$gid}")->field('gname,shopprice,pic')->find();
            //调用hd的购物车类
            //dd($oneGoodsData);die;
            $data = [
                'id'        => $gid, // 商品 ID
                'name'      => $oneGoodsData['gname'],// 商品名称
                'num'       => $data['num'], // 商品数量
                'price'     => $oneGoodsData['shopprice'], // 商品价格
                //'pic'     => $oneGoodsData['pic'], // 商品图片
                'options'   => $data['options'],// 其他参数如价格、颜色、可以为数组或字符串
            ];
            //实例化hd的cart类
            (new Cart())->add($data);
            //(new Cart())->add($data);


            //1.2判断用户是否登录,登录就将添加的购物车商品写入cart数据库
            if(session('home.uid')){
                //存在就重组数据压入过购物车表
                //获得用户id
                $uid  =  session('home.uid');
                //获得属性和规格
                $options = implode(' ',$_POST['options']);
                //$options = implode(',',$_POST['options']);
             //1.3实例化模型查询判断数据库的商品中是否有相同于购物车的商品
                $cartModel  = m('cart');
                $oldData = $cartModel->where("gid = $gid and options= '{$options}' and uid = $uid")->find();
                //$oldData   = $cartModel->where("gid= $gid  and options='{$options}' and uid=$uid")->find();
                //echo 3;DIE;
             //1.4没有就(重组数据$data)执行添加
                if(!$oldData){
                    $data  = [
                        'name'=>$oneGoodsData['gname'],
                        'gid'=>$gid,
                        'num'=>$data['num'],
                        'price'=>$oneGoodsData['shopprice'],
                        'pic'=>$oneGoodsData['pic'],
                        'options'=>$options,
                        'uid'=> $uid,
                        'total'=>$oneGoodsData['shopprice']*$data['num'],
                    ];
                    //执行添加
                    $cartModel->add($data);
                }else{
                    //1.5如果跟当前的数据相同就直接更新数量和价格就行
                    $num  = $oldData['num'] + $data['num'];
                    $temp  = [
                        'num'  =>$num,
                        'total'=> $num*$oldData['price']
                        //'price' =>$num*$data['price']
                    ];

                    //执行更新
                    $cartModel->where("caid={$oldData['caid']}")->save($temp);
                }


            }


            //给一个标识
            echo 1;die;

        }

    }


}